home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / graphics / gnuplot / term / x11.trm < prev    next >
Text File  |  1993-09-15  |  9KB  |  315 lines

  1. /*
  2.  * $Id: x11.trm%v 3.50 1993/07/09 05:35:24 woo Exp $
  3.  *
  4.  */
  5.  
  6. /*
  7.  *    x11.trm  --- inboard terminal driver for X11
  8.  */
  9.  
  10. #define X11_XMAX 4096
  11. #define X11_YMAX 4096
  12.  
  13. /* approximations for typical font/screen sizes */
  14. #define X11_VCHAR (X11_YMAX/25) 
  15. #define X11_HCHAR (X11_XMAX/100) 
  16. #define X11_VTIC (X11_YMAX/100)
  17. #define X11_HTIC (X11_XMAX/150)
  18.  
  19. int X11_Display = 0; /* non-zero if '-display' found on command line */
  20.  
  21. #define X11_nopts 27
  22. char X11_opts[X11_nopts][20] = {
  23.    "-mono", "-gray", "-clear", "-tvtwm", "-pointsize",
  24.    "-iconic", "-rv", "-reverse", "+rv", "-synchronous", 
  25.    "-display", "-geometry", "-bg", "-background", "-bd", "-bordercolor", "-bw",
  26.    "-borderwidth", "-fg", "-foreground", "-fn", "-font", "-name", 
  27.    "-selectionTimeout", "-title", "-xnllanguage", "-xrm" 
  28.    };
  29. int X11_optarg[X11_nopts] = { 
  30.    0, 0, 0, 0, 1,
  31.    0, 0, 0, 0, 0,
  32.    1, 1, 1, 1, 1, 1, 1,
  33.    1, 1, 1, 1, 1, 1, 
  34.    1, 1, 1, 1
  35.    };
  36.  
  37. FILE *X11_ipc; 
  38. char X11_command[1024]= "gnuplot_x11";
  39.  
  40. /*   X11_args - scan gnuplot command line for standard X Toolkit options */
  41.  
  42. X11_args(argc, argv) int argc; char *argv[]; {
  43.    int nx11 = 0, n;
  44.  
  45.    while(++argv, --argc > 0) {
  46.       for (n=0; n<X11_nopts; n++) {
  47.      if (!strcmp(*argv, X11_opts[n])) {
  48.         strcat(X11_command, " ");
  49.         strcat(X11_command, *argv); 
  50.         !strcmp(*argv, "-display") && X11_Display++;
  51.         if (X11_optarg[n]) {
  52.            if (--argc <= 0) return(nx11);
  53.            strcat(X11_command, " \"");
  54.            strcat(X11_command, *++argv); 
  55.            strcat(X11_command, "\"");
  56.            nx11++;
  57.            }
  58.         nx11++; break;
  59.         }
  60.      }
  61.       if (n == X11_nopts) break; 
  62.       }
  63.    return(nx11);
  64.    }
  65.  
  66. /*-----------------------------------------------------------------------------
  67.  *   Three different versions of the remainder of the X11 terminal driver
  68.  *   are provided to support three different types of IPC with the
  69.  *   gnuplot_x11 outboard terminal driver:
  70.  * 
  71.  *   DEFAULT_X11:      popen() pipe for most un*x platforms
  72.  *
  73.  *   CRIPPLED_SELECT : file IPC for un*x platforms with incomplete or faulty
  74.  *                     implementation of BSD select()
  75.  *
  76.  *   VMS :             mailbox/spawn IPC
  77.  *---------------------------------------------------------------------------*/
  78.  
  79. #define DEFAULT_X11
  80. #if defined(VMS) || defined(CRIPPLED_SELECT)
  81. #undef DEFAULT_X11
  82. #endif
  83. #if defined(VMS) && defined(CRIPPLED_SELECT)
  84. Error. Incompatible options.
  85. #endif
  86.  
  87.  
  88. #ifdef DEFAULT_X11
  89. /*-----------------------------------------------------------------------------
  90.  *   DEFAULT_X11 popen() pipe IPC
  91.  *---------------------------------------------------------------------------*/
  92. FILE *popen();
  93.  
  94. X11_init() { X11_ipc = popen(X11_command, "w"); }
  95.  
  96. X11_graphics() { 
  97.    fprintf(X11_ipc, "G\n"); 
  98.    fprintf(X11_ipc, "P7%04d%04d\n", /* size of point symbols */
  99.        term_tbl[term].h_tic / 2, term_tbl[term].v_tic / 2); 
  100. #ifdef ULTRIX_KLUDGE
  101.    fflush(X11_ipc);
  102. #endif
  103.    }
  104.  
  105. X11_text() { 
  106.    fprintf(X11_ipc, "E\n"); fflush(X11_ipc);
  107. #ifdef ULTRIX_KLUDGE
  108.    fprintf(X11_ipc, "E\n"); fflush(X11_ipc);
  109. #endif
  110.    }
  111.  
  112. X11_reset() { fprintf(X11_ipc, "R\n"); fflush(X11_ipc); pclose(X11_ipc); }
  113.  
  114. X11_move(x,y) unsigned int x,y; { fprintf(X11_ipc, "M%04d%04d\n", x, y); }
  115.  
  116. X11_vector(x,y) unsigned int x,y; { fprintf(X11_ipc, "V%04d%04d\n", x, y); }
  117.  
  118. X11_linetype(lt) int lt; { fprintf(X11_ipc, "L%04d\n", lt); }
  119.  
  120. X11_put_text(x,y,str) unsigned int x,y; char str[]; {
  121.    fprintf(X11_ipc, "T%04d%04d%s\n", x, y, str);
  122.    }
  123.  
  124. X11_justify_text(mode) enum JUSTIFY mode; {
  125.    fprintf(X11_ipc, "J%04d\n", mode);
  126.    return(TRUE);
  127.    }
  128.  
  129. X11_point(x,y,number) unsigned int x,y; int number; {
  130.    if (number>=0)
  131.       number %= POINT_TYPES;
  132.    number += 1;
  133.    fprintf(X11_ipc, "P%01d%04d%04d\n", number, x, y);
  134.    }
  135.  
  136. #endif /* DEFAULT_X11 */
  137.  
  138.  
  139. #ifdef CRIPPLED_SELECT
  140. /*-----------------------------------------------------------------------------
  141.  *   CRIPPLED_SELECT file IPC
  142.  *---------------------------------------------------------------------------*/
  143.  
  144. char X11_tmp[32], X11_tmp0[32], X11_shutdown[32];
  145. int X11_pid;
  146.  
  147. X11_init() { 
  148.    if (!(X11_pid = fork())) {
  149.       execl("/bin/sh", "sh", "-c", X11_command, NULL);
  150.       _exit(1);
  151.       }
  152.    sprintf(X11_tmp, "/tmp/Gnuplot_%d", X11_pid);
  153.    sprintf(X11_tmp0, "%s-", X11_tmp);
  154.    sprintf(X11_shutdown, "echo R >%s", X11_tmp);
  155.    }
  156.  
  157. X11_graphics() { 
  158.    X11_ipc = fopen(X11_tmp0, "w"); 
  159.    if (!X11_ipc) { perror(X11_tmp0); system(X11_shutdown); exit(1); }
  160.    fprintf(X11_ipc, "G\n"); 
  161.    fprintf(X11_ipc, "P7%04d%04d\n", /* size of point symbols */
  162.        term_tbl[term].h_tic / 2, term_tbl[term].v_tic / 2); 
  163. #ifdef ULTRIX_KLUDGE
  164.    fflush(X11_ipc);
  165. #endif
  166.    }
  167.  
  168. X11_text() { 
  169.    fprintf(X11_ipc, "E\n"); 
  170. #ifdef ULTRIX_KLUDGE
  171.    fprintf(X11_ipc, "E\n");
  172. #endif
  173.    fclose(X11_ipc);
  174.    rename(X11_tmp0, X11_tmp);
  175.    }
  176.  
  177. X11_reset() { system(X11_shutdown); }
  178.  
  179. X11_move(x,y) unsigned int x,y; { fprintf(X11_ipc, "M%04d%04d\n", x, y); }
  180.  
  181. X11_vector(x,y) unsigned int x,y; { fprintf(X11_ipc, "V%04d%04d\n", x, y); }
  182.  
  183. X11_linetype(lt) int lt; { fprintf(X11_ipc, "L%04d\n", lt); }
  184.  
  185. X11_put_text(x,y,str) unsigned int x,y; char str[]; {
  186.    fprintf(X11_ipc, "T%04d%04d%s\n", x, y, str);
  187.    }
  188.  
  189. X11_justify_text(mode) enum JUSTIFY mode; {
  190.    fprintf(X11_ipc, "J%04d\n", mode);
  191.    return(TRUE);
  192.    }
  193.  
  194. X11_point(x,y,number) unsigned int x,y; int number; {
  195.    if (number>=0)
  196.       number %= POINT_TYPES;
  197.    number += 1;
  198.    fprintf(X11_ipc, "P%01d%04d%04d\n", number, x, y);
  199.    }
  200. #endif /* CRIPPLED_SELECT */
  201.  
  202.  
  203. #ifdef VMS
  204. /*-----------------------------------------------------------------------------
  205.  *   VMS mailbox/spawn IPC - Yehavi Bourvine - YEHAVI@VMS.HUJI.AC.IL
  206.  *---------------------------------------------------------------------------*/
  207.  
  208. #include <iodef.h>
  209. #include <descrip.h>
  210. #define MAILBOX "PLOT_X11$MAILBOX"
  211.  
  212. int vaxc$errno;
  213. static short X11_channel;
  214. static $DESCRIPTOR(lognamedsc,MAILBOX);
  215.  
  216. X11_init() {
  217.  
  218.    /* Create a descriptor for the command. $DESCRIP doesn't work in 
  219.    this context... */
  220.    struct { 
  221.       short size, type; 
  222.       char *address;
  223.       } pgmdsc = { strlen(X11_command), 0, X11_command };
  224.  
  225.  
  226.    /* Create a mailbox which will be used as a pipe for commands to the 
  227.    subprocess.  What we'll write to it will be read by the subprocess as 
  228.    its STDIN. */
  229.    vaxc$errno = sys$crembx(0,&X11_channel,128,128,0,0,&lognamedsc,0);
  230.    if (!(vaxc$errno)&1) {
  231.       printf("SYS$CreMbx failed with status=%d\r\n", vaxc$errno);
  232.       os_error("sys$crembx failed",NO_CARET);
  233.       }
  234.  
  235.    /* Assign an I/O channel to it */
  236.    vaxc$errno = sys$assign(&lognamedsc,&X11_channel,0,0,0);
  237.    if (!(vaxc$errno & 1)) {
  238.       printf("SYS$Assign failed with status=%d\r\n", vaxc$errno);
  239.       os_error("sys$crembx failed",NO_CARET);
  240.       }
  241.  
  242.    /* Create a subprocess whose input is this mailbox. */
  243.    vaxc$errno = lib$spawn(&pgmdsc,&lognamedsc,0,&1,0,0,0,0,0,0,0,0,0);
  244.    if (!((vaxc$errno) & 1)) {
  245.       printf("LIB$SPAWN failed with status=%d\r\n", vaxc$errno);
  246.       os_error("lib$spawn failed",NO_CARET);
  247.       }
  248.    }
  249.  
  250. /*   We use $QIO in order to avoid buffering problems, although it might 
  251.  *   work  as well with simple Fprintf calls.  */
  252.  
  253. X11_vmsqiow(buf) char *buf; {
  254.    int status = sys$qiow(0, X11_channel, IO$_WRITEVBLK, 0, 0, 0, 
  255.              buf, strlen(buf), 0, 0, 0, 0);
  256.    if((status & 0x1) == 0) exit(status);
  257.    }
  258.  
  259. char   X11_vmsbuf[512];
  260.  
  261. X11_graphics() { 
  262.    sprintf(X11_vmsbuf, "G\n");
  263.    X11_vmsqiow(X11_vmsbuf);
  264.    sprintf(X11_vmsbuf, "P7%04d%04d\n", /* size of point symbols */
  265.        term_tbl[term].h_tic / 2, term_tbl[term].v_tic / 2); 
  266.    X11_vmsqiow(X11_vmsbuf);
  267.    }
  268.  
  269. X11_text() {
  270.    sprintf(X11_vmsbuf, "E\n");
  271.    X11_vmsqiow(X11_vmsbuf);
  272.    }
  273.  
  274. X11_reset() { 
  275.    sprintf(X11_vmsbuf, "R\n");
  276.    X11_vmsqiow(X11_vmsbuf);
  277.    sleep(2);        /* Wait for subprocess to finish */
  278.    sys$dassgn(X11_channel);
  279.    }
  280.  
  281. X11_move(x,y) unsigned int x,y; { 
  282.    sprintf(X11_vmsbuf, "M%04d%04d\n", x, y);
  283.    X11_vmsqiow(X11_vmsbuf);
  284.    }
  285.  
  286. X11_vector(x,y) unsigned int x,y; { 
  287.    sprintf(X11_vmsbuf, "V%04d%04d\n", x, y);
  288.    X11_vmsqiow(X11_vmsbuf);
  289.    }
  290.  
  291. X11_linetype(lt) int lt; { 
  292.    sprintf(X11_vmsbuf, "L%04d\n", lt);
  293.    X11_vmsqiow(X11_vmsbuf);
  294.    }
  295.  
  296. X11_put_text(x,y,str) unsigned int x,y; char str[]; { 
  297.    sprintf(X11_vmsbuf, "T%04d%04d%s\n", x, y, str);
  298.    X11_vmsqiow(X11_vmsbuf);
  299.    }
  300.  
  301. X11_justify_text(mode) enum JUSTIFY mode; { 
  302.    sprintf(X11_vmsbuf, "J%04d\n", mode);
  303.    X11_vmsqiow(X11_vmsbuf);
  304.    return(TRUE);
  305.    }
  306.  
  307. X11_point(x,y,number) unsigned int x,y; int number; {
  308.    if (number>=0)
  309.       number %= POINT_TYPES;
  310.    number += 1;
  311.    sprintf(X11_vmsbuf, "P%01d%04d%04d\n", number, x, y);
  312.    X11_vmsqiow(X11_vmsbuf);
  313.    }
  314. #endif /* VMS */
  315.